//
//  NewfeatureViewController.m
//  SinaWeibo
//
//  Created by user on 15/10/19.
//  Copyright © 2015年 ZT. All rights reserved.
//

#import "NewfeatureViewController.h"
#import "MainViewController.h"

#import "NewfeatureModel.h"
#import "WHttpTool.h"
#import "UIImageView+WebCache.h"

@interface NewfeatureViewController () <UIScrollViewDelegate>

@property (nonatomic, weak) UIPageControl *pageControl;

@end

@implementation NewfeatureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
//    NSMutableDictionary *dicparameters = [NSMutableDictionary dictionary];
//    dicparameters[@"stype"] = @"ios";
//    dicparameters[@"var"] = @"3.2.30";
//
//    [WHttpTool GET:NewFeature_URL parameters:dicparameters success:^(id responseObject) {

//        NSDictionary *dic = responseObject;

//        NewfeatureModel *model = [NewfeatureModel objectWithKeyValues:dic];

        // 1.创建scrollView
        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
        [self.view addSubview:scrollView];
        
        // 2.添加图片到scrollView中
        CGFloat scrollW = scrollView.width;
        CGFloat scrollH = scrollView.height;
        
        NSArray *arrayOfImages = [NSArray arrayWithObjects:@"Load_Img01.png",@"Load_Img02.png",@"Load_Img03.png",@"Load_Img04.png", nil];
        
        for (int i = 0; i < arrayOfImages.count; i++) {
            UIImageView *imageView = [[UIImageView alloc] init];
            imageView.frame = CGRectMake(i * scrollW, 0, scrollW, scrollH);
            
//            [imageView sd_setImageWithURL:[model.imgData[i] url] placeholderImage:[UIImage imageNamed:arrayOfImages[i]]];
            imageView.image = [UIImage imageNamed:arrayOfImages[i]];
            [scrollView addSubview:imageView];
            if (i == arrayOfImages.count - 1) {
                [self setupLastImageView:imageView];
            }
        }
        
        // 3.设置scrollView属性
        scrollView.contentSize = CGSizeMake(arrayOfImages.count * scrollW, 0);
        scrollView.showsHorizontalScrollIndicator = NO;
        scrollView.bounces = NO;
        scrollView.pagingEnabled = YES;
        scrollView.delegate = self;
        
        // 4.添加pageControl，用于分页
        
        UIPageControl *pageControl = [[UIPageControl alloc] init];
        pageControl.numberOfPages = arrayOfImages.count;
        pageControl.currentPageIndicatorTintColor = WRGBColor(253, 98, 42);
        pageControl.pageIndicatorTintColor = WRGBColor(189, 189, 189);
        pageControl.centerX = scrollW * 0.5;
        pageControl.centerY = scrollH - 50;
        [self.view addSubview:pageControl];
        self.pageControl = pageControl;

//    } failure:^(NSError *error) {
//        WLog(@"失败%@",error);
//    }];

}

- (void)setupLastImageView:(UIImageView *)imageView
{
    CGRect rect =[[UIScreen mainScreen]bounds];
    // 开启交互
    imageView.userInteractionEnabled = YES;
    
    UIButton *enterBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    CGFloat tmpH =(rect.size.height==480.0f)?30:0;

    if (iPhone5SE) {
        tmpH = -15;
    }
    enterBtn.frame =CGRectMake((rect.size.width-170)/2,rect.size.height*0.81+tmpH+20, 170, 45);
    [imageView addSubview:enterBtn];
    [enterBtn setImage:[UIImage imageNamed:@"btn"] forState:UIControlStateNormal];
    [enterBtn addTarget:self action:@selector(enterAppButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)enterAppButtonPressed:(UIButton *)shareBtn
{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    window.rootViewController = [[MainViewController alloc] init];
}

#pragma mark - UIScrollViewDelegate
// 该代理方法能监听当前scroll的更多属性
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    double page = scrollView.contentOffset.x / scrollView.width;
    // 四舍五入计算出页码
    // 1.3四舍五入 1.3 + 0.5 = 1.8 强转为整数(int)1.8= 1
    // 1.5四舍五入 1.5 + 0.5 = 2.0 强转为整数(int)2.0= 2
    // 1.6四舍五入 1.6 + 0.5 = 2.1 强转为整数(int)2.1= 2
    // 0.7四舍五入 0.7 + 0.5 = 1.2 强转为整数(int)1.2= 1
    self.pageControl.currentPage = (int)(page + 0.5);
}

/*
 1.程序启动会自动加载叫做Default的图片
 1> 3.5inch 非retain屏幕：Default.png
 2> 3.5inch retina屏幕：Default@2x.png
 3> 4.0inch retain屏幕: Default-568h@2x.png
 
 2.只有程序启动时自动去加载的图片, 才会自动在4inch retina时查找-568h@2x.png
 */

/*
 一个控件用肉眼看不见，有哪些可能
 1.根本没有创建实例化这个控件
 2.没有设置尺寸
 3.控件的颜色跟父控件的背景色一样（实际上已经显示了，只不过用肉眼看不见）
 4.透明度alpha <= 0.01
 5.hidden = YES
 6.没有添加到父控件中
 7.被其他控件挡住了
 8.位置不对
 9.父控件发生了以上情况
 10.特殊情况
 * UIImageView没有设置image属性，或者设置的图片名不对
 * UILabel没有设置文字，或者文字颜色和跟父控件的背景色一样
 * UITextField没有设置文字，或者没有设置边框样式borderStyle
 * UIPageControl没有设置总页数，不会显示小圆点
 * UIButton内部imageView和titleLabel的frame被篡改了，或者imageView和titleLabel没有内容
 * .....
 
 添加一个控件的建议（调试技巧）：
 1.最好设置背景色和尺寸
 2.控件的颜色尽量不要跟父控件的背景色一样
 */


@end
